// class Promises {
//   static PENDING = "pending"
//   static FULFILLED = "fulfilled"
//   static REJECTED = "rejected"
//   fulfillCallback = []
//   rejectCallback = []
//   constructor(excutor) {
//     PromisesState = PENDING
//     PromisesResult = null
//     excutor(this.resolve.bind(this), this.reject.bind(this))
//   }
//   resolve(result) {
//     if (PromisesState !== PENDING) return
//     PromisesState = FULFILLED
//     PromisesResult = result
//     while (this.fulfillCallback.length != 0) {
//       this.fulfillCallback.unshift()()
//     }
//   }
//   reject(reason) {
//     if (PromisesState !== PENDING) return
//     PromisesState = REJECTED
//     PromisesResult = reason
//     while (this.rejectCallback.length != 0) {
//       this.rejectCallback.unshift()()
//     }
//   }
//   then(onFulfilled, onRejected) {
//     onFulfilled =
//       typeof onFulfilled === "function" ? onFulfilled : (result) => result
//     onRejected =
//       typeof onRejected === "function"
//         ? onRejected
//         : (reason) => {
//             throw reason
//           }
//     const thenPromises = new Promises((resolve, reject) => {
//       let resolvePromises = (cb) => {
//         queueMicrotask(() => {
//           try {
//             let x = cb(this.PromisesResult)
//             //判断是不是本身
//             if (thenPromises === x) {
//               throw Error("不能返回本身")
//             }
//             //判断是不是promise
//             if (x instanceof Promises) {
//               x.then(resolve, reject)
//             } else {
//               resolve(x)
//             }
//           } catch (err) {
//             throw err
//           }
//         })
//       }
//       if (PromisesState === FULFILLED) {
//         resolvePromises(onFulfilled)
//       } else if (PromisesState === REJECTED) {
//         resolvePromises(onRejected)
//       } else if (PromisesState === PENDING) {
//         this.fulfillCallback.push(resolvePromises.bind(this, onFulfilled))
//         this.rejectCallback.push(resolvePromises.bind(this, onRejected))
//       }
//     })
//     return thenPromises
//   }
// }

//
function all(arr) {
  let res = []
  return new Promise((resolve, reject) => {
    const addData = (item, index) => {
      res[index] = item
      if (res.length == arr.length) {
        resolve(res)
      }
    }
    arr.forEach((item, index) => {
      if (item instanceof Promise) {
        item.then((val) => addData(val, index), reject)
      } else {
        addData(item, index)
      }
    })
  })
}
